package com.itheima.wms.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pinda.base.BaseController;
import com.itheima.pinda.base.R;
import com.itheima.pinda.base.entity.SuperEntity;
import com.itheima.wms.dto.GoodsDTO;
import com.itheima.wms.entity.GoodsEntity;
import com.itheima.wms.service.GoodsService;
import com.itheima.wms.vo.GoodsDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * 货品管理
 * </p>
 */
@Slf4j
@RestController
@RequestMapping("/goods")
@Api(value = "Goods", tags = "货品管理")
public class GoodsController extends BaseController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 分页查询货品管理
     *
     * @param data 分页查询对象
     * @return 查询结果
     */
    @ApiOperation(value = "分页查询货品管理", notes = "分页查询货品管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", dataType = "long", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页显示几条", dataType = "long", paramType = "query", defaultValue = "10"),
            @ApiImplicitParam(name = "ascs", value = "正序排列字段（逗号分隔）", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "descs", value = "倒叙排列字段（逗号分隔）", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "data", value = "无效参数 字段可以使用前缀控制查询方式 gt_, ge_, lt_, le_, in_, like_, likeRight_, likeLeft_", dataType = "string", paramType = "none"),
            @ApiImplicitParam(name = "goodsTypeId", value = "货品类型ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "barCode", value = "条码", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "ownerId", value = "货主名称", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "inspectionType", value = "质检方式", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "temperatureType", value = "温度要求 常温:CW、冷藏:LC、恒温:HW", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "bearingType", value = "承重要求 重型:ZX、中型:OX、轻型:QX", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "volume", value = "体积", dataType = "java.math.BigDecimal", paramType = "query"),
            @ApiImplicitParam(name = "warehouseId", value = "仓库id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "areaId", value = "库区id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "price", value = "单价", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "unit", value = "单位", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "guaranteeDay", value = "保质天数", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createUser", value = "创建人id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "createName", value = "创建人", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "java.time.LocalDateTime", paramType = "query"),
            @ApiImplicitParam(name = "updateUser", value = "更新人id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "updateName", value = "更新人", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "java.time.LocalDateTime", paramType = "query"),
            @ApiImplicitParam(name = "logicDel", value = "逻辑删除 0 删除", dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/page")
    public R<IPage<GoodsEntity>> page(@RequestParam Map data) {
        Page<GoodsEntity> page = getPage();
        IPage<GoodsEntity> goodsPage = goodsService.page(page, data);
        page.setRecords(goodsPage.getRecords());
        return success(page);
    }

    @ApiOperation(value = "查询全部货品管理", notes = "查询全部货品管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ascs", value = "正序排列字段（逗号分隔）", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "descs", value = "倒叙排列字段（逗号分隔）", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "data", value = "无效参数 字段可以使用前缀控制查询方式 gt_, ge_, lt_, le_, in_, like_, likeRight_, likeLeft_", dataType = "string", paramType = "none"),
            @ApiImplicitParam(name = "goodsTypeId", value = "货品类型ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "barCode", value = "条码", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "ownerId", value = "货主名称", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "inspectionType", value = "质检方式", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "temperatureType", value = "温度要求 常温:CW、冷藏:LC、恒温:HW", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "bearingType", value = "承重要求 重型:ZX、中型:OX、轻型:QX", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "volume", value = "体积", dataType = "java.math.BigDecimal", paramType = "query"),
            @ApiImplicitParam(name = "warehouseId", value = "仓库id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "areaId", value = "库区id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "price", value = "单价", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "unit", value = "单位", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "guaranteeDay", value = "保质天数", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createUser", value = "创建人id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "createName", value = "创建人", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "java.time.LocalDateTime", paramType = "query"),
            @ApiImplicitParam(name = "updateUser", value = "更新人id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "updateName", value = "更新人", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "java.time.LocalDateTime", paramType = "query"),
            @ApiImplicitParam(name = "logicDel", value = "逻辑删除 0 删除", dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/list")
    public R<List<GoodsEntity>> list(@RequestParam Map data) {
        List<GoodsEntity> goodsArray = goodsService.list(data);
        return success(goodsArray);
    }

    @ApiOperation(value = "分页查询明细货品", notes = "分页查询明细货品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", dataType = "long", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页显示几条", dataType = "long", paramType = "query", defaultValue = "10"),
            @ApiImplicitParam(name = "ascs", value = "正序排列字段（逗号分隔）", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "descs", value = "倒叙排列字段（逗号分隔）", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "owner", value = "货主名称/编码", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "goods", value = "库位名称/编码/条码", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "ownerName", value = "货主名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "ownerCode", value = "货主编码", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "货品名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "货品编码", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "ownerId", value = "货主id", dataType = "String", paramType = "query"),
    })
    @GetMapping("/pageDetail")
    public R<IPage<GoodsDetailVO>> pageDetail(@RequestParam Map data) {
        Page<GoodsDetailVO> page = getPage();
        IPage<GoodsDetailVO> stockPage = goodsService.pageDetail(page, data);
        page.setRecords(stockPage.getRecords());
        return success(page);
    }

    @ApiOperation(value = "模糊查询货品", notes = "模糊查询货品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "params", value = "参数", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "ownerId", value = "货主id", dataType = "String", paramType = "query"),
    })
    @GetMapping("/vague")
    public R<List<GoodsEntity>> list(String params, Long ownerId) {
        LambdaQueryWrapper<GoodsEntity> wrapper = new LambdaQueryWrapper();
        wrapper.like(StringUtils.isNotBlank(params), GoodsEntity::getCode, params).or().like(StringUtils.isNotBlank(params), GoodsEntity::getName, params).or().like(StringUtils.isNotBlank(params), GoodsEntity::getBarCode, params);
        wrapper.eq(null != ownerId, GoodsEntity::getOwnerId, ownerId);
        List<GoodsEntity> goodsArray = goodsService.list(wrapper);
        return success(goodsArray);
    }

    /**
     * 查询货品管理
     *
     * @param id 主键id
     * @return 查询结果
     */
    @ApiOperation(value = "查询货品管理", notes = "查询货品管理")
    @GetMapping("/{id}")
    public R<GoodsEntity> get(@PathVariable Long id) {
        return success(goodsService.getById(id));
    }

    /**
     * 新增货品管理
     *
     * @param data 新增对象
     * @return 新增结果
     */
    @ApiOperation(value = "新增货品管理", notes = "新增货品管理不为空的字段")
    @PostMapping
    public R<GoodsEntity> save(@RequestBody @Validated(SuperEntity.Save.class) GoodsDTO data) {
        goodsService.save(data);
        return success(data);
    }

    /**
     * 修改货品管理
     *
     * @param data 修改对象
     * @return 修改结果
     */
    @ApiOperation(value = "修改货品管理", notes = "修改货品管理不为空的字段")
    @PutMapping
    public R<GoodsEntity> update(@RequestBody @Validated(SuperEntity.Update.class) GoodsDTO data) {
        goodsService.updateById(data);
        return success(data);
    }

    /**
     * 删除货品管理
     *
     * @param ids 主键id
     * @return 删除结果
     */
    @ApiOperation(value = "删除货品管理", notes = "根据id物理删除货品管理")
    @DeleteMapping
    public R<Boolean> delete(@RequestParam("ids[]") List<Long> ids) {
        goodsService.removeByIds(ids);
        return success();
    }

}
